/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.spider.web.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.spider.web.domain.SpiderWeb;
import me.zhengjie.spider.web.service.SpiderWebService;
import me.zhengjie.spider.web.service.dto.SpiderWebQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.spider.web.service.dto.SpiderWebDto;

/**
* @website https://eladmin.vip
* @author 雨来
* @date 2023-07-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "爬虫网站管理")
@RequestMapping("/api/spiderWeb")
public class SpiderWebController {

    private final SpiderWebService spiderWebService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('spiderWeb:list')")
    public void exportSpiderWeb(HttpServletResponse response, SpiderWebQueryCriteria criteria) throws IOException {
        spiderWebService.download(spiderWebService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询爬虫网站")
    @ApiOperation("查询爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:list')")
    public ResponseEntity<PageResult<SpiderWebDto>> querySpiderWeb(SpiderWebQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(spiderWebService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/queryAll")
    @Log("查询爬虫网站")
    @ApiOperation("查询爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:list')")
    public ResponseEntity<List<SpiderWebDto>> queryAll(SpiderWebQueryCriteria criteria){
        return new ResponseEntity<>(spiderWebService.queryAll(criteria), HttpStatus.OK);
    }

    @GetMapping("/queryById/{webId}")
    @Log("查询爬虫网站")
    @ApiOperation("查询爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:list')")
    public ResponseEntity<SpiderWebDto> queryById(@PathVariable Long webId){
        return new ResponseEntity<>(spiderWebService.findById(webId), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增爬虫网站")
    @ApiOperation("新增爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:add')")
    public ResponseEntity<Object> createSpiderWeb(@Validated @RequestBody SpiderWeb resources){
        spiderWebService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改爬虫网站")
    @ApiOperation("修改爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    public ResponseEntity<Object> updateSpiderWeb(@Validated @RequestBody SpiderWeb resources){
        spiderWebService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除爬虫网站")
    @ApiOperation("删除爬虫网站")
    @PreAuthorize("@el.check('spiderWeb:del')")
    public ResponseEntity<Object> deleteSpiderWeb(@RequestBody Long[] ids) {
        spiderWebService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("爬取大概信息")
    @PostMapping("/spiderAction")
    public ResponseEntity<Object> spiderAction(@Validated @RequestBody SpiderWeb resources) {
        return spiderWebService.spiderAction(resources);
    }

    @ApiOperation("爬取漫画大概信息")
    @PostMapping("/spiderComicAction")
    public ResponseEntity<Object> spiderComicAction(@Validated @RequestBody SpiderWeb resources) {
        return spiderWebService.spiderComicAction(resources);
    }

    @ApiOperation("爬取详细信息")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    @PostMapping("/spiderDetailAction")
    public ResponseEntity<Object> spiderDetailAction(@Validated @RequestBody SpiderWeb resources) {
        return spiderWebService.spiderDetailAction(resources);
    }

    @ApiOperation("爬取漫画详细信息")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    @PostMapping("/spiderComicDetailAction")
    public ResponseEntity<Object> spiderComicDetailAction(@Validated @RequestBody SpiderWeb resources) {
        return spiderWebService.spiderComicDetailAction(resources);
    }

    @ApiOperation("爬取漫画内容信息")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    @PostMapping("/spiderComicContentAction")
    public ResponseEntity<Object> spiderComicContentAction(@Validated @RequestBody SpiderWeb resources) {
        return spiderWebService.spiderComicContentAction(resources);
    }

    @ApiOperation("重置缓存")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    @GetMapping("/resetRedisCache/{webId}")
    public ResponseEntity<Object> resetRedisCache(@PathVariable Long webId) {
        return spiderWebService.resetRedisCache(webId);
    }

    @ApiOperation("重置漫画缓存")
    @PreAuthorize("@el.check('spiderWeb:edit')")
    @GetMapping("/resetComicRedisCache/{webId}")
    public ResponseEntity<Object> resetComicRedisCache(@PathVariable Long webId) {
        return spiderWebService.resetComicRedisCache(webId);
    }

    @ApiOperation("缓存当前网站的所有视频封面")
    @GetMapping("/cacheImgByWebId/{webId}")
    public ResponseEntity<Object> cacheImgByWebId(@PathVariable Long webId) {
        return spiderWebService.cacheImgByWebId(webId);
    }

    @ApiOperation("缓存当前网站的所有漫画封面")
    @GetMapping("/cacheComicImgByWebId/{webId}")
    public ResponseEntity<Object> cacheComicImgByWebId(@PathVariable Long webId) {
        return spiderWebService.cacheComicImgByWebId(webId);
    }

    @ApiOperation("缓存当前网站的所有视频播放文件")
    @GetMapping("/cacheM3U8ByWebId/{webId}")
    public ResponseEntity<Object> cacheM3U8ByWebId(@PathVariable Long webId) {
        return spiderWebService.cacheM3U8ByWebId(webId);
    }

    @ApiOperation("更新当前网站的所有视频播放文件")
    @GetMapping("/overwriteM3u8File/{webId}")
    public ResponseEntity<Object> overwriteM3u8File(@PathVariable Long webId) {
        return spiderWebService.overwriteM3u8File(webId);
    }

}